package com.zhenyi.liuxue.admin;

import java.util.List;

import com.cjw.model.DBMap;
import com.jfinal.kit.JsonKit;
import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.SqlPara;
import com.liuxue.common.model.LxCompany;
import com.liuxue.common.model.LxCompanySchool;
import com.liuxue.common.model.LxSchool;
import com.liuxue.common.model.LxUkSchool;

public class CompanyController extends AdminController {

	public void list() {
		String keyword = getPara("keyword");
		Kv kv = Kv.create();
		if (StrKit.notBlank(keyword)) {
			kv.set("keyword", keyword);
			setAttr("keyword", keyword);
		}
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXAdminFindCompany", kv);
		Page<LxCompany> pages = LxCompany.dao.paginate(getPage(), getPageSize(), sqlPara);

		setRainbow(pages);
		setItems(JsonKit.toJson(pages.getList()));
		render("/lx/admin/company/index.html");
	}

	public void info() {
		setReferer();
		SqlPara sqlPara = Db.use("liuxue").getSqlPara("LXFindSchools");
		List<LxSchool> schools = LxSchool.dao.find(sqlPara);
		Long companyId = getParaToLong("companyId");
		if (companyId != null) {
			LxCompany info = LxCompany.dao.findById(companyId);
			if (info == null || info.getStatus().equals(-1)) {
				renderFailure(404, "invalid id");
				return;
			}
			setInfoItem(info);
		} else {
			setInfoItem(new DBMap());
		}
		setData("schools", schools);
		SqlPara signSqlPara = db.getSqlPara("LXFindCompanySignSchools", Kv.by("companyId", companyId));
		List<LxSchool> signSchools = LxSchool.dao.find(signSqlPara);
		setData("signSchools", signSchools);

		SqlPara companySqlPara = db.getSqlPara("LXAdminFindCompany");
		List<LxCompany> companies = LxCompany.dao.find(companySqlPara);
		setData("companies", companies);

		render("/lx/admin/company/info.html");
	}

	public void submit() {
		Long id = getParaToLong("id");
		String name = getPara("name");
		String englishName = getPara("englishName");
		Integer status = getParaToInt("status", 1);
		Long companyId = getParaToLong("companyId");

		LxCompany info = null;
		if (id == null) {
			info = new LxCompany();
			info.setStatus(status);
			info.save();
		} else {
			info = LxCompany.dao.findById(id);
			if (info == null) {
				renderFailure(404, "not found");
				return;
			}
		}
		info.setName(name);
		info.setEnglishName(englishName);
		info.setStatus(status);
		info.setCompanyId(companyId);

		SqlPara signSqlPara = db.getSqlPara("LXFindCompanySignSchools", Kv.by("companyId", id));
		List<LxSchool> signSchools = LxSchool.dao.find(signSqlPara);
		for (LxSchool lxSchool : signSchools) {
			LxCompanySchool companySchool = LxCompanySchool.dao.findById(id, lxSchool.getId());
			companySchool.delete();
		}

		String signSchoolIds = getPara("signSchoolIds");
		if (StrKit.notBlank(signSchoolIds)) {
			String[] ids = signSchoolIds.split(";");
			for (String string : ids) {
				Long exisitSchoolId = Long.parseLong(string);
				LxCompanySchool companySchool = new LxCompanySchool();
				companySchool.setSchoolId(exisitSchoolId);
				companySchool.setCompanyId(id);
				companySchool.save();
			}
		}
		renderFlag(info.update());
	}

	public void delete() {
		Long id = getParaToLong("id");
		if (id == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxCompany info = LxCompany.dao.findById(id);
		if (info == null) {
			renderFailure(404, "not found");
			return;
		}
		info.setStatus(-1);
		renderFlag(info.update());
	}

	public void schoolCompanies() {
		Long ukSchoolId = getParaToLong("ukSchoolId");
		if (ukSchoolId == null) {
			renderFailure(500, "id is null");
			return;
		}
		LxUkSchool ukSchool = LxUkSchool.dao.findById(ukSchoolId);
		LxSchool school = LxSchool.dao.searchFirst("unionSchoolId", ukSchool.getUnionSchoolId());

		Kv kv = Kv.by("schoolId", school.getId());
		SqlPara sqlPara = db.getSqlPara("LXFindSchoolSignCompanies", kv);
		Page<LxCompany> pages = LxCompany.dao.paginate(getPage(), getPageSize(), sqlPara);

		SqlPara spCompany = db.getSqlPara("LXFindCompanies");
		setAttr("companies", JsonKit.toJson(LxCompany.dao.find(spCompany)));

		setRainbow(pages);
		setItems(JsonKit.toJson(pages.getList()));
		setAttr("ukSchoolId", ukSchoolId);
		setAttr("schoolId", school.getId());
		setAttr("schoolName", school.getName());
		render("/lx/admin/ukSchools/companies.html");
	}

	public void signCompanySchool() {
		Long companyId = getParaToLong("companyId");
		Long schoolId = getParaToLong("schoolId");
		if (companyId == null) {
			renderFailure(500, "companyId is null");
			return;
		}
		if (schoolId == null) {
			renderFailure(500, "school id is null");
			return;
		}

		LxCompanySchool companySchool = LxCompanySchool.dao.findById(companyId, schoolId);
		if (companySchool == null) {
			companySchool = new LxCompanySchool();
			companySchool.setCompanyId(companyId);
			companySchool.setSchoolId(schoolId);
			companySchool.save();
		}
		renderSuccess();
	}

	public void unsignCompanySchool() {
		Long companyId = getParaToLong("companyId");
		Long schoolId = getParaToLong("schoolId");
		if (companyId == null) {
			renderFailure(500, "companyId is null");
			return;
		}
		if (schoolId == null) {
			renderFailure(500, "school id is null");
			return;
		}

		LxCompanySchool companySchool = LxCompanySchool.dao.findById(companyId, schoolId);
		if (companySchool == null) {
			renderFailure(404, "not found");
			return;
		}
		renderFlag(companySchool.delete());
	}

}
